package com.clkj.nangang.mvp.personcenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.clkj.nangang.R;
import com.clkj.nangang.common.Constant;
import com.clkj.nangang.mvp.base.BaseFragment;
import com.clkj.nangang.mvp.login.LoginActivity;
import com.clkj.nangang.mvp.personcenter.personinfo.PersonInfoActivity;
import com.clkj.nangang.util.ActivityManager;
import com.clkj.nangang.util.CommonUtil;
import com.clkj.nangang.util.LogUtil;

/**
 * 政策指南中 办事要件，政策解读，服务指南 下的父级资讯类型
 * Created by Administrator on 2017/7/4.
 */

public class FragmentPerson extends BaseFragment implements View.OnClickListener {

    //默认的是isFirstVisibles true ,刚进来的时候分两种情况
    //1.如果切过来的时候 setVisibleHint在OnCreate之前调用，那isFirstVisible为true，这个时候不进行任何操作；
    //等方法执行到onActivityCreated的时候，在执行一次setUserVisibleHint 这个时候把默认的是isFirstVisibles 设为false
    //然后这个时候调用setUserVISIBLEhINT为true  去获取数据
    //2.如果是后面再次切过来，那isFirstVisible已经为false，那就可以直接通过setUserVisibleHint获取数据了

    //代表这个界面是不是第一次被使用
    boolean isFirstUsed = true;


    String mNewsKindBelong;

    private static final String NEWS_BELONG = "news_belong";
    private TextView tvTitle;
    private TextView tvLogout;
    private TextView tvName;
    private TextView tvPersonInfo;
    private TextView tvMyAppeal;
    private TextView tvMyTrends;
    private TextView tvMyExam;
    private TextView tvAboutUs;
    private TextView tvUpdate;

    public static FragmentPerson newInstance(String newsBelong) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(NEWS_BELONG, newsBelong);

        FragmentPerson fragment = new FragmentPerson();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mNewsKindBelong = bundle.getString(NEWS_BELONG);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person_center, container, false);
        assignView(view);
        initData();
        initView();

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //fragment 可见的情况下 进行加载数据。不可见则不加载
        if (getUserVisibleHint() == true) {
            if (isFirstUsed == true) {
                isFirstUsed = false;
                setUserVisibleHint(true);
            }
            //不可见，说明fragment已经初始话完成,就直接把isFirstVisible改为false 再次切过来的时候就直接可以加载数据了
        } else {
            isFirstUsed = false;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        // LogUtil.e("CartVisible");
        if (getUserVisibleHint()) {
            LogUtil.e("MyShuoShuoVisible");
            //如果不是firstvisible才去进行下面的操作  因为第一次很可能出现各种问题
            if (isFirstUsed == false) {

            }
        } else {
            LogUtil.e("faXianFragmentVisible");
        }
    }

    @Override
    public void initData() {

        String name = (String) CommonUtil.getShareValue(Constant.NANGAN_NAME, "");
        tvName.setText(name);
    }

    @Override
    public void assignView(View view) {

        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        tvLogout = (TextView) view.findViewById(R.id.tv_right);

        tvName = (TextView) view.findViewById(R.id.tv_name);
        tvPersonInfo = (TextView) view.findViewById(R.id.tv_person_info);
        tvMyAppeal = (TextView) view.findViewById(R.id.tv_my_appeal);
        tvMyTrends = (TextView) view.findViewById(R.id.tv_my_trends);
        tvMyExam = (TextView) view.findViewById(R.id.tv_my_exam);
        tvAboutUs = (TextView) view.findViewById(R.id.tv_about_us);
        tvUpdate = (TextView) view.findViewById(R.id.tv_update);

    }

    @Override
    public void initView() {
        tvTitle.setText("个人中心");
        tvLogout.setText("注销登录");
        tvLogout.setOnClickListener(this);

        tvPersonInfo.setOnClickListener(this);
        tvMyAppeal.setOnClickListener(this);
        tvMyTrends.setOnClickListener(this);
        tvMyExam.setOnClickListener(this);

        tvAboutUs.setOnClickListener(this);
        tvUpdate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            //注销
            case R.id.tv_right:
                CommonUtil.clearAllShareValue();
                CommonUtil.putShareValue(Constant.LOGIN_STATUS, false);
                ActivityManager.finishAll();

                Intent toLogin = new Intent(getActivity(), LoginActivity.class);
                startActivity(toLogin);
                break;
            //个人信息
            case R.id.tv_person_info:
                intent = new Intent(getActivity(), PersonInfoActivity.class);
                startActivity(intent);
                break;
            //我的诉求
            case R.id.tv_my_appeal:

                break;

            //我的动态
            case R.id.tv_my_trends:

                break;

            //我的考试
            case R.id.tv_my_exam:

                break;

            //关于我们
            case R.id.tv_about_us:

                break;

            //版本更新
            case R.id.tv_update:

                break;
        }
    }
}

